package net.xdclass.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.util.BeanUtil;
import net.xdclass.enums.AddressStatusEnums;
import net.xdclass.interceptor.LoginInterceptor;
import net.xdclass.mapper.AddressMapper;
import net.xdclass.model.AddressDO;
import net.xdclass.model.LoginUser;
import net.xdclass.request.AddressAddReqeust;
import net.xdclass.service.AddressService;
import net.xdclass.vo.AddressVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressMapper addressMapper;

    @Override
    public AddressDO detail(Long id) {
        return addressMapper.selectOne(new QueryWrapper<AddressDO>().eq("id", id));
    }

    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    @Override
    public int add(AddressAddReqeust addressAddReqeust) {
        LoginUser loginUser = (LoginUser) LoginInterceptor.threadLocal.get();
        AddressDO addressDO = new AddressDO();
        addressDO.setCreateTime(new Date());
        BeanUtils.copyProperties(addressAddReqeust, addressDO);
        addressDO.setUserId(loginUser.getId());

        // 如果是默認地址
        if (addressDO.getDefaultStatus() == AddressStatusEnums.DEFAULT_STATUS.getCode()) {
            AddressDO defaultAddress = addressMapper.selectOne(new QueryWrapper<AddressDO>()
                    .eq("user_id", loginUser.getId())
                    .eq("default_status", AddressStatusEnums.DEFAULT_STATUS.getCode())
            );

            if (defaultAddress != null) {
                defaultAddress.setDefaultStatus(AddressStatusEnums.COMMON_STATUS.getCode());
                addressMapper.update(defaultAddress, new QueryWrapper<AddressDO>().eq("user_id", loginUser.getId()));
            }

        }

        int row = addressMapper.insert(addressDO);
        return row;
    }

    public AddressVO findById(long addressId) {
        LoginUser loginUser = (LoginUser)LoginInterceptor.threadLocal.get();
        AddressDO addressDO = addressMapper.selectOne(new QueryWrapper<AddressDO>().eq("id", addressId).eq("user_id", loginUser.getId()));

        if (addressDO == null) {
            return null;
        }
        AddressVO addressVO = new AddressVO();
        BeanUtils.copyProperties(addressDO, addressVO);

        return addressVO;
    }

    @Override
    public List<AddressVO> listUserAllAddress() {
        LoginUser loginUser = (LoginUser) LoginInterceptor.threadLocal.get();
        List<AddressDO> list = addressMapper.selectList(new QueryWrapper<AddressDO>().eq("user_id", loginUser.getId()));

        List<AddressVO> result = list.stream().map(obj -> {
            AddressVO addressVO = new AddressVO();
            BeanUtils.copyProperties(obj, addressVO);
            return addressVO;
        }).collect(Collectors.toList());

        return result;
    }
}
